//import CostItem from "../controller/ui/cost_items"

import { Skill } from "../class/skill"
import RoomType from "../entity/RoomType"
import DD from "../manager/dynamic_data_manager"
import JsonManager from "../manager/json_manager"
import PoolManager from "../manager/pool_manager"
import StorageManager from "../manager/storage_manager"
import { Emitter } from "../utils/emmiter"
import { MessageType } from "../utils/message"
import { Utils } from "../utils/utils"

// import UIAnimaManager from "../manager/action_manager"

const { ccclass, property } = cc._decorator

interface OkCalBack {
    (isOK: boolean, param?: {}): void
}

@ccclass
export default class EndPanelUIManager extends cc.Component {
    static instance: EndPanelUIManager = null

    @property(cc.Node)
    uiNode: cc.Node = null

    @property(cc.Node)
    maskNode: cc.Node = null

    @property(cc.Label)
    labelTitle: cc.Label = null

    @property(cc.Label)
    labelDesc: cc.Label = null

    @property(cc.Button)
    btn1: cc.Button = null


    @property(cc.Node)
    content1: cc.Node = null; // ScrollView 的 Content 节点
    @property(cc.Button)
    buttonConfirm: cc.Button = null

    @property(cc.Label)
    labelCancel: cc.Label = null

    @property(cc.Label)
    labelTicket: cc.Label = null
    @property(cc.Label)
    labelConfirm: cc.Label = null

    ids: string[] = []

    curCallBack: OkCalBack = null
    curParam: any = null


    _isClick: boolean = false

    onLoad() {
        // console.log("PoolManager Onload");
        EndPanelUIManager.instance = this

        //this.maskNode.on('click', this.hideUI, this);

        this.btn1.node.on("click", this.returnToLevelList, this)

        // this.buttonConfirm.node.on("click", this.confirm, this)

        if (this.uiNode.active) {
            this.uiNode.active = false
        }
    }

    populateScrollView() {
        this.content1.removeAllChildren(); // 清空之前的内容
        let bonusData = DD.instance.winItems
        // PoolManager
       this.labelTicket.string=( DD.instance.roomType==RoomType.Elite?20:10).toString()
        bonusData.forEach((data, index) => {
            let item = PoolManager.instance.createObjectByName('list_btn_num', this.content1)
            // 创建每一项

            const labels = item.getComponentsInChildren(cc.Label);
            let label = labels[0]
            if (label) {
                label.string = `${data}`;
            }
            let label1 = labels[1]
            // 设置节点名称，便于定位
            item.name = `Item-${index}`;
            label1.string = '50'
            // this.content1.addChild(item); // 添加到 Content
        });
        this.content1.getComponent(cc.Layout).updateLayout()
    }



    showUI(title: string, desc: string, callback: OkCalBack = null, param: {} = null, isNormal: boolean = true, labels: string[] = null) {
        if (!this.uiNode.active) {
            this.uiNode.active = true
            //  UIAnimaManager.instance.showDialog(this.uiNode, this.maskNode)
        }
        this.labelTitle.string = title
        cc.log(title, desc, param)
        cc.director.pause();
        this.populateScrollView()
        // 获取 canStack 为 0 的 id
        // const cantStackIds = Object.values(JsonManager.instance.getDataByName('skill'))
        //     .filter((item: Skill) => item.canStack === 0) // 筛选出 canStack 为 0 的项
        //     .map((item: Skill) => Number(item.id));              // 提取 id

        // // 找到 skillIds 和 cantStackIds 的重叠部分
        // const overlappingIds = Object.values(StorageManager.instance.skills).map(id => Number(id)).
        //     filter(id => cantStackIds.includes(id));


        // let existIds = Object.values(JsonManager.instance.getDataByName('skill'))
        //     .filter((item: Skill) => item.preId == 0);




        // this.refreshUI()
        this.curParam = param
    }



    returnToLevelList() {
        
        Emitter.fire(MessageType.returnToLevelList)
        this.hideUI()
    }


    confirm() {
        if (this._isClick) {
            return
        }
        this._isClick = true
        this.scheduleOnce(function () {
            this._isClick = false
        }, 0.2)

        if (this.curCallBack != null) {
            this.curCallBack(true, this.curParam)
        }
        this.hideUI()
    }
    hideUI() {
        this.uiNode.active = false
        cc.director.resume();
        //  AudioManager.instance.playAudio('ui_close')

    }
}
